<?php
namespace app\api\controller;
use think\Config;
use think\Controller;
use think\Request;
use app\base\model\User;
use think\Session;
use verify\Send;
use think\Cache;
class Login extends Base
{
    protected $user;
    protected $appid;
    protected $appSecret;
    public function __construct(Request $request)
    {
        $this->user = new User();
        parent::__construct($request);
       // $this->tempId  = 434358;
    }
    /**
     *发送手机验证码
     * return json
     */
    public function sendCode(){
       $phone=$this->request->post('phone');
       $type =$this->request->post('type','register');  //register:注册 forget:忘记密码 login:短信快捷登陆
       if(empty($phone)){
          $msg['messsage']=checkMsg(PHONE_CANNOT_EMPTY);
          return $msg;
       }
        $count=$this->user->where(['phone'=>$phone])->count();
       if($type=='register'){
           if($count>0){
                $msg['message']=checkMsg(PHONE_IS_EXITE);
                return $msg;
           }
       }elseif($type=='forget'){
            if($count<0){
                 $msg['message']=checkMsg(PHONE_IS_NOT_EXIT);
                 return $msg;
            }
       }
        $code=rand(1000,9999);
        $cache_data=[
            'phone' => $phone,
            'send_time' =>time(),
            'code'     =>$code
        ];
        Cache::set($phone.$type,$cache_data,'300');
        $data =[$code,'5'];
         $send_res=$this->sendMobileCode($phone,$data,$this->tempId);
        if($send_res['code']==1){
            $msg['message']=checkMsg(CHECK_CODE_SEND_SUCCESS);
        }else{
            $msg['message']=checkMsg(CHECK_CODE_SEND_FAIL);
        }
        return $msg;
    }
    /**
     *验证手机号码
     * return json
     */
    public function checkPhoneCode(){
        $phone = $this->request->post('phone');
        $code  = $this->request->post('code');
        $type  = $this->request->post('type','register'); //register:注册 forget:忘记密码 login:短信快捷登陆
        if(empty($phone) || empty($code)){
             $msg['messsage']=checkMsg(CANSHU_ERROR);
             return $msg;
        }
        if(!Cache::has($phone.$type)){
            $msg['message']=checkMsg(CHECK_CODE_ERROR);
        }else{
             $cache_data=Cache::get($phone.$type);
             if((time()-$cache_data['send_time'])>300){
                 $msg['message']=checkMsg(CHECK_CODE_EXPRI);
             }elseif ($code!=$cache_data['code']){
                 $msg['message']=checkMsg(CHECK_CODE_ERROR);
             }else{
                 $msg['message']=checkMsg(CHECK_CODE_SUCCESS);
             }
        }
        return $msg;
    }
    /**
     *用户注册
     * return 接送
     */
    public function register(){
        $input=$this->request->param();
        $check_result=$this->validate($input,'Login.register');
        if($check_result!==true){
            $msg['message']= checkMsg($check_result);
            return $msg;
        }
        if($input['password']!=$input['re_password']){
             $msg['message']=checkMsg(PASSWORD_DIFF);
            return $msg;
        }
        $data=[
            'phone'   => $input['phone'],
            'password'=> MD5($input['password']),
            'add_time'=>time()
        ];
        $res=$this->user->insertGetId($data);
        if($res){
            $user_name=$this->createUserName(6);
            $this->user->save(['user_name'=>$user_name],['uid'=>$res]);
            $msg['message']= checkMsg(REGISTER_SUCCESS);
        }else{
            $msg['message']=checkMsg(REGISTER_FAIL);
        }
        return $msg;
    }
    /**
     *用户账号密码登陆
     * return json
     */
    public function user_login(){
        $input = $this->request->param();
        $check_code = $this->validate($input,'Login.login');
        if($check_code!==true){
             $msg['message']=checkMsg($check_code);
            return $msg;
        }
        $user_info=$this->user->where(['phone'=>$input['phone']])->find();
        if(empty($user_info)){
             $msg['message']= checkMsg(USER_NOT_REG);
            return $msg;
        }
        if($user_info['status']==0){
            $msg['message']=checkMsg(USER_IS_FREEZE);
            return $msg;
        }
        if(MD5($input['password'])!==$user_info['password']){
             $msg['message']=checkMsg(PASSWORD_ERROR);
            return $msg;
        }else{
           $session_id=GetRandStr(50);
           Session::set($session_id,$user_info,'api');
           $msg=checkMsg(LOGIN_SUCCESS);
           return ['message'=>$msg,'session_id'=>$session_id,'user_name'=>$user_info['user_name']];
        }
    }
    /**
     *用户短信快捷登录
     * return json
     */
        public function code_login(){
        $phone=$this->request->post('phone');
        $code =$this->request->post('code');
         if(empty($phone) || empty($code)){
             $msg['message']=checkMsg(CANSHU_ERROR);
             return $msg;
         }
        if(!Cache::has($phone.'login')){
             $msg['message']=checkMsg(CHECK_CODE_ERROR);
            return $msg;
        }else{
            $cache_data=Cache::get($phone.'login');
            if((time()-$cache_data['send_time'])>300){
                $msg['message']=checkMsg(CHECK_CODE_EXPRI);
            }elseif ($code!=$cache_data['code']){
                $msg['message']=checkMsg(CHECK_CODE_ERROR);
            }else{
                 $userInfo=$this->user->where(['phone'=>$phone])->find();
                 if(empty($userInfo)){  //该手机号第一次登录
                    $user_name=$this->createUserName(6);
                    $userInfo=[
                        'phone'   =>$phone,
                        'user_name'=>$user_name,
                        'add_time'=>time()
                    ];
                    $res=$this->user->insertGetId($userInfo);
                    $userInfo['uid']=$res;
                 }else{
                     if($userInfo['status']==0){
                         $msg['message']=checkMsg(USER_IS_FREEZE);
                         return $msg;
                     }
                     $user_name=$userInfo['user_name'];
                 }
                $session_id=GetRandStr(50);
                Session::set($session_id,$userInfo,'api');
                $msg['message']=checkMsg(LOGIN_SUCCESS);
                $msg['session_id']=$session_id;
                $msg['user_name']=$user_name;
            }
            return $msg;
        }
    }
    /**
     *退出登录
     * return json
     */
    public function logout(){
        $session_id  =$this->request->post('session_id');
        if(empty($session_id)){
             $msg['message']=checkMsg(CANSHU_ERROR);
            return $msg;
        }
        Session::delete($session_id,'api');
        Cache::clear();
        $user_info=Session::get($session_id);
        if(empty($user_info)){
            $msg=checkMsg(LOGIN_OUT_SUCCESS);
        }else{
            $msg=checkMsg(LOGIN_OUT_FAIL);
        }
        return ['message'=>$msg];
    }
    /**
     *微信扫描二维码登录
     * return json
     */
    public function wechatLogin(){
      //获取微信公众号二维码

    }

}